package com.bdqn.tour.servlet.comments;



import com.bdqn.tour.dao.HotelImplementationClass;
import com.bdqn.tour.dao.impl.ImplUersDao;
import com.bdqn.tour.entity.Comment;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/XiCha")
public class XiChaServlet extends HttpServlet {
    private static final int RECORDS_PER_PAGE = 5; // 每页显示的记录数
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("utf-8");
       resp.setCharacterEncoding("utf-8");
       resp.setContentType("text/html;charset=utf-8");
        Comment comment=new Comment();
        HotelImplementationClass commentDao = new ImplUersDao();
        int currentPage = 1;

        int start = (currentPage - 1) * RECORDS_PER_PAGE;
        int totalRecords = getTotalRecords(); // 获取总记录数
        int totalPages = (int) Math.ceil((double) totalRecords / RECORDS_PER_PAGE);

       if(req.getParameter("all")!=null){//当前端传递all参数时，显示全部评论


           List<Comment> commentsList = getComments(start, RECORDS_PER_PAGE); // 获取当前页的评论数据

           req.setAttribute("commentsList", commentsList);
           req.setAttribute("currentNo", currentPage);
           req.setAttribute("totalPages", totalPages);

           req.getRequestDispatcher("/HotelReviews.jsp").forward(req, resp);
       }
       if (req.getParameter("double_bed_room")!=null){
           comment.setXin(req.getParameter("double_bed_room"));
           List<Comment> commentsList = commentDao.getXi(start, RECORDS_PER_PAGE,comment.getXin()); // 获取当前页的评论数据

           req.setAttribute("commentsList", commentsList);
           req.setAttribute("currentNo", currentPage);
           req.setAttribute("totalPages", totalPages);

           req.getRequestDispatcher("/HotelReviews.jsp").forward(req, resp);
       }
        if (req.getParameter("standard_room")!=null){
            comment.setXin(req.getParameter("standard_room"));
            List<Comment> commentsList = commentDao.getXi(start, RECORDS_PER_PAGE,comment.getXin()); // 获取当前页的评论数据

            req.setAttribute("commentsList", commentsList);
            req.setAttribute("currentNo", currentPage);
            req.setAttribute("totalPages", totalPages);

            req.getRequestDispatcher("/HotelReviews.jsp").forward(req, resp);
        }

    }
    private int getTotalRecords() {
        HotelImplementationClass commentDao = new ImplUersDao();
        return commentDao.getTotalCommentsCount1();
        // 从数据库中获取总记录数
        // 示例：return commentDao.getTotalRecords();
    }

    private List<Comment> getComments(int start, int limit) {
        HotelImplementationClass commentDao = new ImplUersDao();
        return commentDao.getComments(start, limit);

        // 从数据库中获取指定范围的评论数据
        // 示例：return commentDao.getComments(start, limit);
        // 返回当前页的评论数据
    }
}
